<template>
  <div>
    <nav-header></nav-header>
    <nav-bread>
      <span slot="bread">我的果篮</span>
    </nav-bread>
    <svg style="position: absolute; width: 0; height: 0; overflow: hidden;" version="1.1"
         xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <defs>
        <symbol id="icon-add" viewBox="0 0 32 32">
          <title>添加</title>
          <path class="path1"
                d="M15 17h-13.664c-0.554 0-1.002-0.446-1.002-1 0-0.552 0.452-1 1.002-1h13.664v-13.664c0-0.554 0.446-1.002 1-1.002 0.552 0 1 0.452 1 1.002v13.664h13.664c0.554 0 1.002 0.446 1.002 1 0 0.552-0.452 1-1.002 1h-13.664v13.664c0 0.554-0.446 1.002-1 1.002-0.552 0-1-0.452-1-1.002v-13.664z"></path>
        </symbol>
        <symbol id="icon-ok" viewBox="0 0 32 32">
          <title>确认</title>
          <path class="path1"
                d="M31.020 0.438c-0.512-0.363-1.135-0.507-1.757-0.406s-1.166 0.435-1.529 0.937l-17.965 24.679-5.753-5.67c-0.445-0.438-1.035-0.679-1.664-0.679s-1.219 0.241-1.664 0.679c-0.917 0.904-0.917 2.375 0 3.279l7.712 7.6c0.438 0.432 1.045 0.681 1.665 0.681l0.195-0.008c0.688-0.057 1.314-0.406 1.717-0.959l19.582-26.9c0.754-1.038 0.512-2.488-0.538-3.233z"></path>
        </symbol>
        <symbol id="icon-edit" viewBox="0 0 32 32">
          <title>编辑</title>
          <path class="path1"
                d="M28.287 8.51l-4.805-4.806 0.831-0.831c0.472-0.472 1.086-0.777 1.564-0.777 0.248 0 0.452 0.082 0.622 0.253l3.143 3.144c0.539 0.54 0.133 1.529-0.524 2.186l-0.831 0.831zM26.805 9.992l-1.138 1.138-4.805-4.806 1.138-1.138 4.805 4.806zM24.186 12.612l-14.758 14.762-4.805-4.806 14.758-14.762 4.805 4.806zM7.379 28.288l-4.892 1.224 1.223-4.894 3.669 3.67zM31.123 4.011l-3.143-3.144c-0.567-0.567-1.294-0.867-2.103-0.867-1.036 0-2.174 0.52-3.045 1.391l-20.429 20.436c-0.135 0.134-0.23 0.302-0.276 0.487l-2.095 8.385c-0.089 0.355 0.017 0.736 0.276 0.995 0.198 0.198 0.461 0.307 0.741 0.307 0.085 0 0.171-0.010 0.254-0.031l8.381-2.096c0.185-0.047 0.354-0.142 0.487-0.276l20.43-20.436c1.409-1.41 2.042-3.632 0.524-5.15v0z"></path>
        </symbol>
        <symbol id="icon-del" viewBox="0 0 32 32">
          <title>删除</title>
          <path class="path1"
                d="M11.355 4.129v-2.065h9.29v2.065h-9.29zM6.194 29.935v-23.742h19.613v23.742h-19.613zM30.968 4.129h-8.258v-3.097c0-0.569-0.463-1.032-1.032-1.032h-11.355c-0.569 0-1.032 0.463-1.032 1.032v3.097h-8.258c-0.569 0-1.032 0.463-1.032 1.032s0.463 1.032 1.032 1.032h3.097v24.774c0 0.569 0.463 1.032 1.032 1.032h21.677c0.569 0 1.032-0.463 1.032-1.032v-24.774h3.097c0.569 0 1.032-0.463 1.032-1.032s-0.463-1.032-1.032-1.032v0z"></path>
          <path class="path2"
                d="M10.323 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"></path>
          <path class="path3"
                d="M16 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"></path>
          <path class="path4"
                d="M21.677 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"></path>
        </symbol>
        <symbol id="icon-clock" viewBox="0 0 32 32">
          <title>clock</title>
          <path class="path1" fill="#605f5f"
                d="M16 29c-7.168 0-13-5.831-13-13s5.832-13 13-13c7.168 0 13 5.832 13 13s-5.832 13-13 13zM16 0c-8.822 0-16 7.178-16 16s7.178 16 16 16c8.822 0 16-7.178 16-16s-7.178-16-16-16z"></path>
          <path class="path2" fill="#605f5f"
                d="M23.958 21.837l-6.958-6.489v-6.282c0-0.827-0.673-1.5-1.5-1.5s-1.5 0.673-1.5 1.5v6.934c0 0.414 0.174 0.814 0.477 1.098l7.435 6.934c0.279 0.259 0.642 0.402 1.023 0.402 0.415 0 0.814-0.174 1.096-0.477 0.564-0.605 0.532-1.555-0.073-2.12z"></path>
        </symbol>
      </defs>
    </svg>
    <div class="container">
      <div class="cart">
        <div class="page-title-normal">
          <h2 class="page-title-h2"><span>我的果篮</span></h2>
        </div>

        <el-table
          ref="selectBasket"
          :data="cartList"
          tooltip-effect="dark"
          style="width: 100%"
          stripe
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column label="水果信息">
            <template slot-scope="scope">
              <img style="width: 50%;height: 50%" v-bind:src="('http://localhost:8080/file/image/'+scope.row.fImage )" v-bind:alt="scope.row.fName">
              <span style="position: absolute;transform: translate3d(30%,100%,0)">{{ scope.row.fName}}</span>
            </template>

          </el-table-column>
          <el-table-column label="原价">
            <template slot-scope="scope"><span v-if="scope.row.FIsGift==false">￥</span>{{(scope.row.fPrice)}}<span v-if="scope.row.FIsGift">积分</span></template>
          </el-table-column>
          <el-table-column label="促销价">
            <template slot-scope="scope">{{(scope.row.pPrice) | currency('￥')}}</template>
          </el-table-column>
          <el-table-column label="重量/件数">
            <template slot-scope="scope" >
              <el-input-number @click="totalPrice()" class="input-sub" size="mini" v-model=scope.row.weight :precision="1" :step="0.1" :min="0.5" :max="10" label="添加重量" style="position: relative;left:4%;width:60%"></el-input-number>
              <span v-if="scope.row.weight > scope.row.stock" style="color: red;position: relative;left: 7%;">库存不足</span>
            </template>
          </el-table-column>
          <el-table-column label="总价">
            <template slot-scope="scope">{{(scope.row.weight * scope.row.pPrice) | currency('￥')}}</template>
          </el-table-column>
          <el-table-column label="管理">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">
                <i class="el-icon-delete"></i> 删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>


<!--        <div class="item-list-wrap">-->
<!--          <div class="cart-item">-->
<!--            <div class="cart-item-head">-->
<!--              <ul>-->
<!--                <li>商品名称</li>-->
<!--                <li>原价</li>-->
<!--                <li>促销价</li>-->
<!--                <li>重量</li>-->
<!--                <li>总价</li>-->
<!--                <li>删除</li>-->
<!--              </ul>-->
<!--            </div>-->
<!--            -->
<!--            <ul class="cart-item-list">-->
<!--              <li v-for="item in cartList">-->
<!--                <div class="cart-tab-1">-->
<!--                  <div class="cart-item-check">-->
<!--                    <a href="javascipt:;" class="checkbox-btn item-check-btn"-->
<!--                       v-bind:class="{'check': item.checked == '1'}" @click="editCart('checked',item)">-->
<!--                      <svg class="icon icon-ok">-->
<!--                        <use xlink:href="#icon-ok"></use>-->
<!--                      </svg>-->
<!--                    </a>-->
<!--                  </div>-->
<!--                  <div class="cart-item-pic">-->
<!--                    <img v-bind:src="('http://localhost:8080/file/image/'+item['fImage'] )" v-bind:alt="item.name">-->
<!--                  </div>-->
<!--                  <div class="cart-item-title">-->
<!--                    <div class="item-name">{{item.fName}}</div>-->
<!--                  </div>-->
<!--                </div>-->
<!--                <div class="cart-tab-2">-->
<!--                  <div class="item-price">{{item.fPrice | currency('￥')}}/Kg</div>-->
<!--                </div>-->
<!--                <div class="cart-tab-2">-->
<!--                  <div class="item-pprice">{{item.pPrice | currency('￥')}}/Kg</div>-->
<!--                </div>-->
<!--                <div class="cart-tab-4">-->
<!--                  <div class="item-quantity">-->
<!--                    <div class="select-self select-self-open">-->
<!--                      <div class="select-self-area">-->
<!--                        <el-input-number class="input-sub" size="mini" v-model="item.weight" :precision="1" :step="0.1" :min="0.5" :max="10" label="添加重量" style="position: relative;left:4%;width:60%"></el-input-number>-->
<!--                      </div>-->
<!--                    </div>-->
<!--                  </div>-->
<!--                </div>-->
<!--                <div class="cart-tab-5">-->
<!--                  <div class="item-price-total">{{(item.weight * item.pPrice) | currency('￥')}}</div>-->
<!--                </div>-->
<!--                <div class="cart-tab-5">-->
<!--                  <div class="cart-item-opration">-->
<!--                    <a href="javascript:;" class="item-edit-btn" @click="delCartConfirm(item)">-->
<!--                      <svg class="icon icon-del">-->
<!--                        <use xlink:href="#icon-del"></use>-->
<!--                      </svg>-->
<!--                    </a>-->
<!--                  </div>-->
<!--                </div>-->
<!--              </li>-->
<!--            </ul>-->
<!--          </div>-->
<!--        </div>-->
        <div class="cart-foot-wrap">
          <div class="cart-foot-inner">
            <div class="cart-foot-l">
              <!-- 批量删除按钮 & 取消选择按钮 -->
              <div>
                <el-button size="small" type="primary" @click="batchDelete">批量删除</el-button>
                <el-button size="small" @click="cancelSelect()">取消选择</el-button>
              </div>
            </div>
            <div class="cart-foot-r">
              <div class="item-total">
                  <span style="color: #ea1515" v-if="totalPrice[1]>points">积分不足</span>
                合计: <span class="total-price">{{totalPrice[0] | currency('￥')}}+{{totalPrice[1]}}积分</span>
              </div>
              <div class="btn-wrap">
                <a class="btn btn--red" v-bind:class="{'btn--dis':selectBasket.length === 0||totalPrice[1]>points}" @click="checkOut">结算</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <nav-footer></nav-footer>
  </div>
</template>

<script>
  import NavHeader from './../components/NavHeader'
  import NavFooter from './../components/NavFooter'
  import NavBread from './../components/NavBread'
  import Modal from './../components/Modal'
  import axios from "axios";
  import {config} from "shelljs";
  import {editList} from "autoprefixer/lib/utils";

  export default {
    data() {
      return {
        checkedCount:0,
        cartList: [],
        modalConfirm: false,
        delItem: '',
        selectBasket: [], //被选中的水果
        points: 0
      }
    },
    components: {
      NavHeader,
      NavFooter,
      NavBread,
      Modal
    },
    mounted() {
      sessionStorage.removeItem("selectBasket");
      this.init();
    },
    computed: {
      checkAllFlag() {
        return this.checkedCount == this.selectBasket.length;
      },
      checkedCount() {
        var i = 0;
        this.selectBasket.forEach(() => {
          i++;
        })
        console.log(i);
        return i;
      },
      totalPrice() {
        var money = 0;
        var point = 0;
        this.selectBasket.forEach((item) => {
          money += parseFloat(item.pPrice) * parseInt(item.weight);
          if (item.FIsGift){
            point += item.fPrice * parseInt(item.weight);
          }
        })
        console.log(money);
        var res = [money,point];
        return res;
      }
    },
    methods: {
      init() {
        this.$axios.get("/customer/getCart?cid="+JSON.parse(sessionStorage.getItem("user")).id).then((result) => {
          let res = result.data;
          if (res.code === 0) {
            console.log(res.data);
            this.cartList = res.data;
            this.getPoints();
          } else {
            this.cartList = [];
          }
        }).catch(function (error) {
          console.log("error init." + error)
        });
      },
      checkOut() {
        if (this.selectBasket.length > 0) {
            sessionStorage.setItem("selectBasket",JSON.stringify(this.selectBasket));
            this.$router.push({
              path: "/address"
            })
        }
      },
      //获得用户积分
      getPoints(){
        axios.get("customer/memberEdit?id="+JSON.parse(sessionStorage.getItem("user")).id)
          .then(res => {
            console.log(res);
            this.points = res.data.integral;
          })
      },
      //单选框的选中状态改变触发的函数 val就是选中的数据 是一个数组
      handleSelectionChange(val) {
        //保存被选中的分类数据
        this.selectBasket = val;
      },
      //批量删除
      batchDelete() {
        //判断是否有选中
        if (!this.selectBasket.length) {
          this.$message.error("请选择后再操作！");
          return;
        }
        //收集需要删除分类的id
        let idString =  JSON.stringify(this.selectBasket.map(v => v.id)).toString();
        //提示框
        this.$confirm("你确定要删除吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let ids= (idString.substring(1,idString.length-1)).split(",");
            let check = 0;
            for (let id in ids){
              console.log(ids[id]);
              axios.get("/customer/deleteBasket?BId=" + ids[id] )
                .then(response => {
                  //接收响应数据
                  let { error_code, reason } = response.data;
                  check += error_code;
                  if (error_code === 0){
                    this.init();
                  }else if(error_code) {
                    //弹出失败提示
                    this.$message.error(reason);
                  }
                })
                .catch(err => {
                  console.log(err);
                });
            }
            if (check === 0){
              //弹出成功提示
              this.$message({
                showClose: true,
                type: "success",
                message: "删除成功！"
              });
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },
      //删除商品函数
      handleDelete(BId) {
        this.$confirm("你确定要删除吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            //发送ajax 传入FId
            console.log(BId);
            axios.get("/customer/deleteBasket?BId=" + BId )
              .then(response => {
                //接收响应数据
                let { error_code, reason } = response.data;
                if (error_code === 0) {
                  //弹出成功提示
                  this.$message({
                    showClose: true,
                    type: "success",
                    message: reason
                  });
                  //请求商品数据(刷新)
                  this.init();
                } else if (error_code) {
                  //弹出失败提示
                  this.$message.error(reason);
                }
              })
              .catch(err => {
                console.log(err);
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },
      //取消选中
      cancelSelect() {
        this.$refs.selectBasket.clearSelection();
      },
    }
  }
</script>

<style>
.cart-item-list .cart-tab-1{
  width: 22%;
}
  .input-sub, .input-add {
    min-width: 40px;
    height: 100%;
    border: 0;
    color: #605F5F;
    text-align: center;
    font-size: 16px;
    overflow: hidden;
    display: inline-block;
    background: #f0f0f0;
  }

  .item-quantity .select-self-area {
    background: none;
    border: 1px solid #f0f0f0;
  }

  .item-quantity .select-self-area .select-ipt {
    display: inline-block;
    padding: 0 3px;
    width: 30px;
    min-width: 30px;
    text-align: center;
  }
</style>
